package com.blackhorse.controller;

import com.blackhorse.domain.Account;
import com.blackhorse.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Account表现层控制器
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired//注入AccountService
    private AccountService accountService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        System.out.println("表现层：查询所有账户...");
        //整合Spring，调用AccountService
        List<Account> accounts = accountService.findAll();
        model.addAttribute("accounts", accounts);
        return "list";
    }

    @RequestMapping("/saveAccount")
    public String saveAccount(Account account) {
        System.out.println("表现层：保存账户...");
        //整合Spring，调用AccountService
        accountService.saveAccount(account);
        //使用关键字转发
        return "forward:/account/findAll";
    }

    @RequestMapping("/transfer")
    public String transfer(int srcId, int tarId, float money) {
        System.out.println("表现层：转账..." + money);
        accountService.transfer(srcId, tarId, money);
        return "forward:/account/findAll";
    }
}
